/*
 * @Author: chyang0822 270917365@qq.com
 * @Date: 2024-10-25 17:30:28
 * @LastEditors: chyang0822 270917365@qq.com
 * @LastEditTime: 2024-10-28 09:26:18
 * @FilePath: /Project-DMAI-Deploy/tutorials/concurrency/listing_2.1.cpp
 * @Description: 
 * 
 * Copyright (c) 2024 by ${git_name_email}, All Rights Reserved. 
 */
#include <thread>
#include <iostream>

void do_something(int& i)
{
    ++i;
}

struct func
{
    int& i;

    func(int& i_):i(i_){}

    void operator()()
    {
        for(unsigned j=0;j<1000000;++j)
        {
            std::cout << "hello world" << std::endl;
            do_something(i);            // 1 潜在访问隐患：悬空引用        
            std::cout << "i:" << i << std::endl;
        }
    }
};


void oops()
{
    int some_local_state=0;
    func my_func(some_local_state);      
    std::thread my_thread(my_func);
    my_thread.detach();                  // 2 不等待线程结束
}                                        // 3 新线程可能还在运行

int main()
{
    oops();
}
